//
//  EducationExperienceViewController.m
//  MagicZhao
//
//  Created by hy002 on 2017/12/6.
//  Copyright © 2017年 HUAYU. All rights reserved.
//

#import "EducationExperienceViewController.h"

static NSString *cellPath = @"educatCell";
@interface EducationExperienceViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic, strong)NSArray *titAry;
@property (nonatomic, strong)NSString *schoolName;
@property (nonatomic, strong)NSString *education;
@property (nonatomic, strong)UILabel *eduLab;
@end

@implementation EducationExperienceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _titAry = @[@"学校",@"学历"];
    _schoolName = @"";
    _education = @"";
    if (_isEdit)
    {
        _schoolName = _schoolNameStr;
        _education = _eduNameStr;
    }
    [self creatUI];
}

- (void)creatUI
{
    self.navigationItem.title = @"教育信息";
    self.view.backgroundColor = BacksColor;
    self.navigationController.navigationBar.translucent = NO;
    UIButton *rightBtn = [ZCControl createButtonWithFrame:CGRectMake(0, 0, 40, 30) ImageName:nil Target:self Action:@selector(saveAction) Title:@"保存" Font:16.0];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    UIBarButtonItem *barbtn = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = barbtn;
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT-SCREER_HeiX) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.backgroundColor = BacksColor;
    [_tableview setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    [self.view addSubview:_tableview];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _titAry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellPath];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = _titAry[indexPath.row];
    cell.textLabel.font = NormalFontWithSize(14.0+fontScale);
    cell.textLabel.textColor = RGB(77, 77, 77);
    if (indexPath.row == 0)
    {
        UITextField *textfield = [ZCControl createTextFieldWithFrame:CGRectMake(100*displaywidth, 5*displaywidth, SCREEN_WIDTH-120*displaywidth, 35*displaywidth) placeholder:@"输入学校名字" passWord:NO leftImageView:nil rightImageView:nil Font:13.0];
        [textfield setValue:RGB(179, 179, 179) forKeyPath:@"_placeholderLabel.textColor"];
        textfield.textColor = RGB(77, 77, 77);
        textfield.textAlignment = NSTextAlignmentRight;
        textfield.text = _schoolName;
        textfield.delegate = self;
        [cell.contentView addSubview:textfield];
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        UILabel *baseLabel = [ZCControl createLabelWithFrame:CGRectMake(100*displaywidth, 2.5*displayheight, SCREEN_WIDTH-130*displaywidth, 40*displayheight) Font:13.0 Text:_education Color:RGB(77, 77, 77)];
        baseLabel.textAlignment = NSTextAlignmentRight;
        [cell.contentView addSubview:baseLabel];
        _eduLab = baseLabel;
        
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 1)
    {
        WEAKSELF;
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"选择学历" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        [alertVC addAction:[UIAlertAction actionWithTitle:@"本科以上" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            weakSelf.education = @"本科以上";
            weakSelf.eduLab.text = @"本科以上";
        }]];
        
        [alertVC addAction:[UIAlertAction actionWithTitle:@"本科" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            weakSelf.education = @"本科";
            weakSelf.eduLab.text = @"本科";
            
            
        }]];
        [alertVC addAction:[UIAlertAction actionWithTitle:@"专科" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            weakSelf.education = @"专科";
            weakSelf.eduLab.text = @"专科";
            
            
        }]]; [alertVC addAction:[UIAlertAction actionWithTitle:@"专科以下" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            weakSelf.education = @"专科以下";
            weakSelf.eduLab.text = @"专科以下";
            
            
        }]];
        
        
        [self presentViewController:alertVC animated:YES completion:nil];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45*displaywidth;
}

//保存
- (void)saveAction
{
    if ([_schoolName isEqualToString:@""])
    {
        [LTools showMBProgressWithText:@"请输入学校名字" addToView:self.view];
        return;
    }
    if ([_education isEqualToString:@""])
    {
        [LTools showMBProgressWithText:@"请选择学历" addToView:self.view];
        return;
    }
    NSDictionary *dic = @{@"education":_education,
                          @"school":_schoolName
                          };
    [self requestUpdateWorkerInfo:dic];
}

- (void)requestUpdateWorkerInfo:(NSDictionary *)dic
{
    WEAKSELF;
    [HYBaseHandler requestUpdateWorkerInfoParmers:dic Success:^(id obj) {
        if ([obj server_Getcodecheck])
        {
            [LTools showMBProgressWithText:@"保存成功" addToView:sharedDelegate.window];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        
    }];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    _schoolName = textField.text;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.view endEditing:YES];
    return indexPath;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
